/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package db;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Saves the precomputation to the database
 * @author Bryan
 */
public class SavePrecomp extends HttpServlet {

    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       Connection con = DBConnector.connect();
        try{
            String precompno = request.getParameter("precompno");
            String client = request.getParameter("clienttype");
            String loan = request.getParameter("loantype");
            String sub = request.getParameter("subtype");
            String pn = request.getParameter("pn");
            int term = Integer.parseInt(request.getParameter("term"));
            String p_ui = request.getParameter("ui");
            String p_sc = request.getParameter("sc");
            String p_evat = request.getParameter("evat");
            String p_dc = request.getParameter("dc");
            String p_cf = request.getParameter("cf");
            String p_pf = request.getParameter("pf");
            String bc = request.getParameter("bc");
            String lbc = request.getParameter("lbc");
            String notary = request.getParameter("notary");
            String cb = request.getParameter("cb");
            String lastname= request.getParameter("lastname");
            String firstname = request.getParameter("firstname");
            String middlename = request.getParameter("middlename");
            
            if(precompno==null || "".equals(precompno)){
                
                PreparedStatement ps = con.prepareStatement("Insert into precomp (clienttype,subtype,"
                        + "loantype,pnmonth,terms,ui,service,evat,doc,collection,process,lastname,firstname,middlename,notary,bankcharge,lbc,cashbound) values "
                        + "(?,?,?,?,?,?/100,?/100,?/100,?/100,?/100,?/100,?,?,?,?,?,?,?)");
                ps.setString(1,client);
                ps.setString(2,sub);
                ps.setString(3,loan);
                ps.setString(4,pn);
                ps.setInt(5,term);
                ps.setString(6,p_ui);
                ps.setString(7,p_sc);
                ps.setString(8,p_evat);
                ps.setString(9,p_dc);
                ps.setString(10,p_cf);
                ps.setString(11,p_pf);
                ps.setString(12,lastname);
                ps.setString(13,firstname);
                ps.setString(14,middlename);
                ps.setString(15,notary);
                ps.setString(16,bc);
                ps.setString(17,lbc);    
                ps.setString(18,cb);
                ps.executeUpdate();
            }else{
                String query = "UPDATE quicklenddb.precomp"
                     + " SET clienttype=?, subtype=?, loantype=?, pnmonth=?, terms=?, ui=?, service=?, evat=?, doc=?, collection=?, process=?, lastname=?, firstname=?, middlename=?, notary=?, bankcharge=?, lbc=?, cashbound=?"
                     + " WHERE precompno = \"" +precompno+ "\";";
                PreparedStatement ps = con.prepareStatement(query);
                
                ps.setString(1,client);
                ps.setString(2,sub);
                ps.setString(3,loan);
                ps.setString(4,pn);
                ps.setInt(5,term);
                ps.setString(6,p_ui);
                ps.setString(7,p_sc);
                ps.setString(8,p_evat);
                ps.setString(9,p_dc);
                ps.setString(10,p_cf);
                ps.setString(11,p_pf);
                ps.setString(12,lastname);
                ps.setString(13,firstname);
                ps.setString(14,middlename);
                ps.setString(15,notary);
                ps.setString(16,bc);
                ps.setString(17,lbc);    
                ps.setString(18,cb);
                ps.executeUpdate();
            }
            
        }catch(Exception e){
            System.out.println(e);
            response.sendRedirect("fail.jsp");
        }
        response.sendRedirect("precomputation.jsp");
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        
}
    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
